@echo off
chcp 65001
echo 开始部署前端...

REM 设置服务器信息
set SERVER=root@linchaochao.com
set REMOTE_DIR=/home/frontend
set PASSWORD=Tx13578@2469
set DOMAIN=linchaochao.com

echo 是否需要开启服务器端口(80,443,3000)？[Y/N]
set /p OPEN_PORTS=
if /i "%OPEN_PORTS%"=="Y" (
    echo 正在开启端口...
    ssh %SERVER% "ufw allow 80/tcp && ufw allow 443/tcp && ufw allow 3000/tcp && echo '端口已开启'"
) else (
    echo 部署已取消
    pause
    exit /b 1
)

REM 检查并升级 Node.js
echo 检查 Node.js 版本...
ssh %SERVER% "curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && apt-get install -y nodejs && node -v && npm -v"

REM 配置 SSH 密钥
echo 配置 SSH 密钥...
if not exist "%USERPROFILE%\.ssh\id_rsa" (
    echo 生成 SSH 密钥...
    echo y | ssh-keygen -t rsa -f "%USERPROFILE%\.ssh\id_rsa" -N ""
    echo 配置服务器 SSH 密钥...
    echo %PASSWORD% | ssh %SERVER% "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" < "%USERPROFILE%\.ssh\id_rsa.pub"
)

REM 检查并安装 Nginx
echo 检查 Nginx 安装状态...
ssh %SERVER% "if command -v nginx &> /dev/null; then echo 'Nginx已安装，版本信息：' && nginx -v; else echo '正在安装Nginx...' && apt-get update && apt-get install -y nginx && echo 'Nginx安装完成，版本信息：' && nginx -v; fi"

REM 创建临时 Nginx 配置文件
echo 配置 Nginx...
echo server { > nginx.conf.temp
echo     listen 80; >> nginx.conf.temp
echo     server_name %DOMAIN%; >> nginx.conf.temp
echo. >> nginx.conf.temp
echo     # API 代理 >> nginx.conf.temp
echo     location /api/ { >> nginx.conf.temp
echo         proxy_pass http://127.0.0.1:8080/api/; >> nginx.conf.temp
echo         proxy_http_version 1.1; >> nginx.conf.temp
echo         proxy_set_header Upgrade $http_upgrade; >> nginx.conf.temp
echo         proxy_set_header Connection "upgrade"; >> nginx.conf.temp
echo         proxy_set_header Host $host; >> nginx.conf.temp
echo         proxy_cache_bypass $http_upgrade; >> nginx.conf.temp
echo         proxy_set_header X-Real-IP $remote_addr; >> nginx.conf.temp
echo         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; >> nginx.conf.temp
echo         proxy_set_header X-Forwarded-Proto $scheme; >> nginx.conf.temp
echo         proxy_set_header Origin ""; >> nginx.conf.temp
echo         proxy_read_timeout 300; >> nginx.conf.temp
echo         proxy_connect_timeout 300; >> nginx.conf.temp
echo     } >> nginx.conf.temp
echo. >> nginx.conf.temp
echo     # 前端应用代理 >> nginx.conf.temp
echo     location / { >> nginx.conf.temp
echo         proxy_pass http://127.0.0.1:3000; >> nginx.conf.temp
echo         proxy_http_version 1.1; >> nginx.conf.temp
echo         proxy_set_header Upgrade $http_upgrade; >> nginx.conf.temp
echo         proxy_set_header Connection "upgrade"; >> nginx.conf.temp
echo         proxy_set_header Host $host; >> nginx.conf.temp
echo         proxy_cache_bypass $http_upgrade; >> nginx.conf.temp
echo         proxy_set_header X-Real-IP $remote_addr; >> nginx.conf.temp
echo         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; >> nginx.conf.temp
echo         proxy_set_header X-Forwarded-Proto $scheme; >> nginx.conf.temp
echo         proxy_read_timeout 300; >> nginx.conf.temp
echo         proxy_connect_timeout 300; >> nginx.conf.temp
echo     } >> nginx.conf.temp
echo } >> nginx.conf.temp

REM 上传并应用 Nginx 配置
scp nginx.conf.temp %SERVER%:/etc/nginx/conf.d/frontend.conf
del nginx.conf.temp

REM 测试并重启 Nginx
echo 测试并重启 Nginx...
ssh %SERVER% "nginx -t && systemctl restart nginx"

REM 检查并安装 pm2
echo 检查 pm2 安装状态...
ssh %SERVER% "if command -v pm2 &> /dev/null; then echo 'PM2已安装，版本信息：' && pm2 -v; else echo '正在安装PM2...' && npm install -g pm2 && echo 'PM2安装完成，版本信息：' && pm2 -v; fi"

REM 创建压缩文件
echo 创建压缩文件...
if exist frontend.tar.gz del frontend.tar.gz
echo 正在打包文件...
tar -czf frontend.tar.gz public src package.json package-lock.json next.config.mjs ecosystem.config.js tsconfig.json tailwind.config.ts postcss.config.mjs next-env.d.ts middleware.ts .env.production .env.development
echo 检查打包文件...
dir frontend.tar.gz

REM 创建远程目录
echo 创建远程目录...
ssh %SERVER% "rm -rf %REMOTE_DIR% && mkdir -p %REMOTE_DIR%"

REM 传输压缩文件
echo 传输压缩文件...
scp frontend.tar.gz %SERVER%:%REMOTE_DIR%/

REM 在服务器上解压并安装依赖
echo 在服务器上解压并安装依赖...
ssh %SERVER% "cd %REMOTE_DIR% && echo '解压文件...' && tar -xzf frontend.tar.gz && echo '清理压缩包...' && rm frontend.tar.gz && echo '检查文件...' && ls -la && echo '清理旧的构建文件...' && rm -rf .next && rm -rf node_modules && echo '开始安装依赖...' && npm install && echo '开始构建...' && npm run build && echo '检查构建结果...' && ls -la .next && echo '开始启动应用...' && pm2 delete frontend || true && pm2 start ecosystem.config.js --env production && echo '等待应用启动...' && sleep 5 && echo '检查端口状态：' && netstat -tlnp | grep :3000 && echo '查看应用日志：' && pm2 logs frontend --lines 20"

REM 清理本地压缩文件
echo 清理本地压缩文件...
del frontend.tar.gz

REM 重启服务
echo 重启服务...
ssh %SERVER% "cd %REMOTE_DIR% && echo '检查应用状态...' && pm2 list && echo '重启应用...' && pm2 restart frontend && echo '重启 Nginx...' && systemctl restart nginx && sleep 2 && echo '检查进程和端口...' && pm2 list && netstat -tlnp | grep :3000 && echo '检查 Nginx 状态...' && systemctl status nginx"

REM 检查后端状态
echo 检查后端状态...
ssh %SERVER% "echo '后端目录内容：' && ls -la /home/backend && echo '运行中的Java进程：' && ps aux | grep java && echo '后端端口监听状态：' && netstat -tlnp | grep 8443 || true"

REM 如果后端没有运行，启动后端
echo 是否需要启动后端？[Y/N]
set /p START_BACKEND=
if /i "%START_BACKEND%"=="Y" (
    echo 启动后端...
    ssh %SERVER% "cd /home/backend && nohup java -jar new-0.0.1-SNAPSHOT.jar > backend.log 2>&1 &"
    echo 等待后端启动...
    timeout /t 10 /nobreak
    echo 检查后端状态...
    ssh %SERVER% "echo '运行中的Java进程：' && ps aux | grep java | grep -v grep && echo '后端端口监听状态：' && netstat -tlnp | grep 8443"
)

echo 部署完成！
echo 您的应用已经部署在 http://%DOMAIN%
pause 